package com.zn.task;

import com.alibaba.fastjson.JSONObject;
import com.zn.entity.OptionTradingContract;
import com.zn.mapper.OptionAccountMapper;
import com.zn.mapper.OptionTradingContractMapper;
import com.zn.service.OptionTradeService;
import com.zn.util.Constant;
import com.zn.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author YinJiaWei
 * @version 2018/11/14 10:56
 * 风控强平定时任务
 */
@Component
public class RiskManagementCloseOrder {
    private static final Logger LOGGER = LoggerFactory.getLogger(RiskManagementCloseOrder.class);
    @Autowired
    private OptionTradingContractMapper optionTradingContractMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private OptionTradeService optionTradeService;
    @Autowired
    private OptionAccountMapper optionAccountMapper;
    private Integer version=0;

    /**
     * 当日14点开始强平
     */
    @Scheduled(cron="0 0 14 * * ?")
    public void beginClosePosition() throws Exception{
        /**循环10次**/
        for(int i=0;i<10;i++){
            doClosePosition(1);
            /**停止3min**/
            Thread.sleep(180000);
        }
    }

    /**
     * 早上09.31分开始持续平仓
     */
    @Scheduled(cron="0 31 09 * * ?")
    public void continuedClosePosition() throws Exception{
        for(int i=0;i<10;i++){
            doClosePosition(2);
            /**停止3min**/
            Thread.sleep(180000);
        }
    }

    /**
     * 早上8.30将昨天持仓已满和开仓已满的状态置为 未满
     */
    @Scheduled(cron="0 30 08 * * ?")
    public void updateOptionAccountIsFull(){
        optionAccountMapper.updateOptionAccountIsFull();
    }

//    /**
//     * 测试切换母账户
//     */
//    @Scheduled(cron="0 25 11 * * ?")
//    public void test() throws Exception{
//        optionTradeService.respReportOrder("report|20181219|0|5||0|0|1|1|0|0.0365|a181219100002|10001332||11:16:40|8888|0|0|");
//        optionTradeService.respDealOrder("deal|1|a181129100052|10001440|394037|96230|20181129|14:36:27|2|0.0349|1|0|");
//    }


    /**
     *
     * @param type 1-开始风控平仓 2-持续风控平仓
     */
    private void doClosePosition(Integer type){
        String msg=type==1?"doClosePosition..............begin":"doClosePosition..............continued";
        LOGGER.info(msg);
        String today= DateUtils.convertDateToString(new Date());
        OptionTradingContract optionTradingContractByWhere=new OptionTradingContract();
        optionTradingContractByWhere.setIsTrading(0);
        List<OptionTradingContract> optionTradingContractList=optionTradingContractMapper.selectByWhere(optionTradingContractByWhere);
        /**获取当前交易的月份**/
        for(OptionTradingContract optionTradingContract:optionTradingContractList){
            String itckKey= Constant.OPTION_TICKL_INFO+optionTradingContract.getTradingGoodsCode()+":"+optionTradingContract.getContractMonthName()+":"+today;
            /**获取当前月份交易的合约**/
            List<Object> tickList=redisTemplate.opsForHash().values(itckKey);
            for(Object object:tickList){
                JSONObject jsonObject=JSONObject.parseObject(object.toString());
                /**获取该合约code**/
                String instrumentCode=jsonObject.getString("instrumentCode");
                /**获取该合约距离交割天数**/
                Integer remainderDays=jsonObject.getInteger("remainderDays");
                /**获取该月份到期提前天数**/
                Integer forceCloseAdvanceDay=optionTradingContract.getForceCloseAdvanceDay();
                /**如果交割距离天数等于提前天数 则开始强平**/
                LOGGER.info("instrumentCode..........."+instrumentCode+",remainderDays:.........."+remainderDays+",forceCloseAdvanceDay.........."+forceCloseAdvanceDay);
                Double limitPrice=jsonObject.getDouble("bidPrice1");
                /**14:00开始风控平仓**/
                if(type==1 && remainderDays==forceCloseAdvanceDay){
//                    LOGGER.info("doClosePosition..............begin"+optionTradingContract.getContractMonthName());
                    //申买价一
                    optionTradeService.riskManagementClose(instrumentCode,limitPrice);
                }
                /**09:31持续风控平仓**/
                if(type==2 && remainderDays<forceCloseAdvanceDay){
//                    LOGGER.info("doClosePosition..............continued"+optionTradingContract.getContractMonthName());
                    optionTradeService.riskManagementClose(instrumentCode,limitPrice);
                }
            }
        }
    }
}
